ntp server

  1. 检查安装,以下命令选一(默认应该会有,基本不用执行步骤1,2)

    1
    2
    3
    4
    5
    6
    # 检查安装
    whereis ntp
    # 检查安装
    rpm -qa | grep ntp
    # 检查安装
    yum list installed | grep ntp
  2. 如果没有ntp,以下命令选一(默认应该会有,基本不用执行步骤1,2)

    1
    2
    3
    4
    # yum 安装
    yum -y install ntp
    # rpm 安装
    rpm -ivh ntp*
  3. root权限,备份/etc/ntp.conf

    1
    2
    mv /etc/ntp.conf /etc/ntp.conf.back
    # cp /etc/ntp.conf /etc/ntp.conf.back
  4. root权限,新建ntp.conf

    1
    vi /etc/ntp.conf
  5. 复制以下内容到/etc/ntp.conf,然后保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
driftfile /var/lib/ntp/drift

# 放行所有进入本 NTP 的服务器,或可以列出单独ip

restrict default ignore

# 默认值,放行本机来源

restrict 127.0.0.1

# 放行局域网用户来源,或可以列出单独IP

restrict 10.160.13.0 mask 255.255.255.0 nomodify notrap

# 默认的一个内部时钟数据,用在没有外部 NTP 服务器时,使用它为局域网用户提供服务:

server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys
  1. 启动ntpd服务,二选一
1
2
3
4
# 方法一
service ntpd start
# 方法二
/etc/init.d/ntpd start
  1. 打开端口(如防火墙没开,不需要开端口)
1
2
3
4
5
6
# 查看123端口
lsof -i:123
# 查看123端口
netstat -anpo| grep 123
# 对外开放123端口
iptables -I INPUT -p udp -m udp --sport 123 -j ACCEPT
  1. 加入自启动
1
chkconfig ntpd on
  1. 查看状态
1
2
3
4
5
6
# ntp服务状态
ntpstat
# 查看ntp服务器与上层ntp的状态,ctrl c 退出
watch ntpq -p
# ntp服务端口
netstat -unlnp

ntp client

  1. 尝试连接ntp服务器
1
ntpdate –d serverIP

如显示stratum 16。为失败,而正常情况下stratum这个值得范围是0~15

  1. 定时任务修改
1
crontab -e
  1. 注释掉原同步ntpdate,增加如下ntpdata 任务(ip为配置好的ntp server ip)
1
2
3
* * * * *  /usr/sbin/ntpdate 10.160.13.110

* * * * * sleep 30; /usr/sbin/ntpdate 10.160.13.110

ntpdate

1
2
#-u:从man ntpdate中可以看出-u参数可以越过防火墙与主机同步;
ntpdate -u 210.72.145.44 :网络时间同步命令

注意:若不加上-u参数, 会出现以下提示:no server suitable for synchronization found